package com.example.fsd.service.news;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.fsd.common.result.R;
import com.example.fsd.pojo.Jdal;
import com.example.fsd.pojo.News;
import com.example.fsd.mapper.NewsMapper;
import com.example.fsd.service.BaseService;
import com.example.fsd.service.news.INewsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.fsd.utils.ImageUpdate;
import com.example.fsd.vo.PageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zhangguihong
 * @since 2024-11-22
 */
@Service
public class NewsServiceImpl extends ServiceImpl<NewsMapper, News> implements INewsService, BaseService {

    @Autowired
    private ImageUpdate imageUpdate;

    @Override
    public R listNewsPage(PageVo pageVo) {
        LambdaQueryWrapper<News> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(News::getIsShow,"1");
        Page<News> page = new Page<>(pageVo.getPageNo(),pageVo.getPageSize());
        IPage<News> newsIPage = this.page(page,lambdaQueryWrapper);
        return R.ok().put("newsIPage",newsIPage);
    }

    @Override
    public R findNewsById(Integer id) {
        return R.ok().put("news",this.getById(id));
    }

    @Override
    public R findNewsnextById(Integer id) {
        LambdaQueryWrapper<News> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.gt(News::getId,id);
        lambdaQueryWrapper.eq(News::getIsShow,"1");
        lambdaQueryWrapper.last("limit 1");
        News one = this.getOne(lambdaQueryWrapper);
        if(one==null){
            return R.error(1404,"没有下一篇");
        }
        return R.ok().put("news",one);
    }

    @Override
    public R findNewsprevById(Integer id) {
        LambdaQueryWrapper<News> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.lt(News::getId,id);
        lambdaQueryWrapper.eq(News::getIsShow,"1");
        lambdaQueryWrapper.orderByDesc(News::getId);
        lambdaQueryWrapper.last("limit 1");
        News one = this.getOne(lambdaQueryWrapper);
        if(one==null){
            return R.error(1404,"没有上一篇");
        }
        return R.ok().put("news",one);
    }

    @Override
    public R updateNewsxqImg(MultipartFile file, Integer id) {
        R r = imageUpdate.updateImage(file);
        String url =(String) r.get("url");
        System.out.println("新闻图片返回地址"+url);
        // 更新数据库
        News news = this.getById(id);
        news.setImgUrl(url);
        this.updateById(news);
        return R.ok().put("url",url);
    }

    @Override
    public R updatenews(News news) {
        this.updateById(news);
        return R.ok();
    }

    @Override
    public R listALlNews() {
        LambdaQueryWrapper<News> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(News::getIsShow,"1").orderByDesc(News::getId);
        return R.ok().put("newsList",this.list(lambdaQueryWrapper));
    }

    @Override
    public R addNews(News news) {
        boolean save = this.save(news);
        if(save){
            return R.ok();
        }else{
            return R.error(1404,"添加失败");
        }
    }

}
